package cn.afactory.tygz.ui.base.utils;

import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.widget.Toast;

/**
 * Created by Kenneth on 15/9/10.
 */
public class ToastUtils {
    private static Toast toast;

    public enum ToastDisplayTime {
        TOAST_DISPLAY_LONG, TOAST_DISPLAY_SHORT
    }

    private ToastUtils() {
    }

    private static void checkToast(Context ctx) {
        if (toast == null) {
            toast = Toast.makeText(ctx, null, Toast.LENGTH_SHORT);
        }
    }

    private static void setToastDisplayTime(ToastDisplayTime time) {
        if (time == ToastDisplayTime.TOAST_DISPLAY_LONG)
            toast.setDuration(Toast.LENGTH_LONG);
        else {
            toast.setDuration(Toast.LENGTH_SHORT);
        }
    }

    public static void show(Context ctx, String msg, ToastDisplayTime time) {
        if (ctx==null){
            return;
        }
        checkToast(ctx);
        toast.setText(msg);
        setToastDisplayTime(time);
        toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
        toast.show();
    }

    public static void show(Context ctx, int msg, ToastDisplayTime time) {
        if (ctx==null){
            return;
        }
        checkToast(ctx);
        toast.setText(msg);
        setToastDisplayTime(time);
        toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
        toast.show();
    }

    public static void show(Context ctx, String msg) {
        if (!TextUtils.isEmpty(msg)) {
            show(ctx, msg, ToastDisplayTime.TOAST_DISPLAY_SHORT);
        }
    }

    public static void show(Context ctx, int msg) {
        show(ctx, msg, ToastDisplayTime.TOAST_DISPLAY_SHORT);
    }
}
